Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(capping): reup capping on connection creation #2674

Merged
merged 3 commits into from
Sep 4, 2024

Conversation

bodinsamuel
Copy link
Collaborator

@bodinsamuel bodinsamuel commented Sep 4, 2024

Describe your changes

Fixes https://linear.app/nango/issue/NAN-1638/capping-on-connection-creation-is-not-working-anymore

  • Reup capping

  • Fix error handling
    Unfortunately, we have mixed public and internal API, during error handling rework it changed the API response for both :/
    It was not broken but just displaying empty error

  • UI: fix error not resetting when changing integration

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for authentication errors, providing more specific error messages.
    • Improved efficiency in connection creation checks by restructuring control flow.
    • Updated middleware for resource capping, enhancing error response clarity.
  • Bug Fixes

    • Resolved issues with stale error messages in the integration creation process.
  • Documentation

    • Updated type definitions to reflect changes in error handling and middleware functions.
  • Chores

    • Refined type safety in middleware function declarations.

@bodinsamuel bodinsamuel self-assigned this Sep 4, 2024
Copy link

linear bot commented Sep 4, 2024

Copy link

coderabbitai bot commented Sep 4, 2024

Walkthrough

The changes involve updates to error handling in the Nango class, modifications to middleware functionality, and enhancements in route type safety. Additionally, there are adjustments to API error types, a removal of resource capping errors from deployment contexts, and improvements in user experience within the frontend integration creation process. These modifications collectively aim to refine error reporting, control flow, and type safety across various components.

Changes

Files Change Summary
packages/frontend/lib/index.ts Updated error handling in Nango class to use more specific error message and code parameters in AuthError.
packages/server/lib/hooks/hooks.ts Added early return in connectionCreationStartCapCheck function if scriptConfigs is empty to improve efficiency.
packages/server/lib/middleware/resource-capping.middleware.ts Renamed authCheck to resourceCapping, improved error handling, and updated error response structure.
packages/server/lib/routes.ts Replaced authCheck with resourceCapping and added type annotations for middleware arrays to enhance type safety.
packages/server/lib/utils/asyncWrapper.ts Changed type parameters in asyncWrapper to accept broader types for request body and query string.
packages/types/lib/api.ts Added ApiError<'resource_capped'> to ResDefaultErrors type for enhanced error handling.
packages/types/lib/flow/http.api.ts Removed ApiError<'resource_capped'> from PostPreBuiltDeploy error types, narrowing the error handling scope.
packages/webapp/src/pages/Connection/Create.tsx Reset serverErrorMessage state to empty string when an existing integration is detected to improve user experience.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Frontend
    participant Middleware
    participant Server

    User->>Frontend: Create Integration
    Frontend->>Middleware: Validate Integration
    Middleware-->>Frontend: Integration Validated
    Frontend->>Server: Setup Connection
    Server-->>Frontend: Connection Setup Successful
    Frontend-->>User: Integration Created
Loading

🐰 In the meadow, I hop with glee,
New changes sprout like a blooming tree.
Errors handled with care, oh what a sight,
Integration flows smooth, everything feels right!
With every hop, I cheer and play,
For clearer paths in code today! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ceee86d and 037ca18.

Files selected for processing (8)
  • packages/frontend/lib/index.ts (7 hunks)
  • packages/server/lib/hooks/hooks.ts (1 hunks)
  • packages/server/lib/middleware/resource-capping.middleware.ts (1 hunks)
  • packages/server/lib/routes.ts (3 hunks)
  • packages/server/lib/utils/asyncWrapper.ts (1 hunks)
  • packages/types/lib/api.ts (1 hunks)
  • packages/types/lib/flow/http.api.ts (1 hunks)
  • packages/webapp/src/pages/Connection/Create.tsx (1 hunks)
Additional comments not posted (18)
packages/server/lib/middleware/resource-capping.middleware.ts (4)

2-2: LGTM!

The code changes are approved.


5-6: LGTM!

The code changes are approved.


7-16: LGTM!

The code changes are approved.


19-20: LGTM!

The code changes are approved.

packages/types/lib/flow/http.api.ts (1)

31-31: LGTM! The code change aligns with the PR objectives.

The removal of the ApiError<'resource_capped'> variant from the Error property of the PostPreBuiltDeploy type is consistent with the PR objectives and the AI-generated summary. This change indicates that the API will no longer handle or report errors related to resource capping in the context of pre-built deployments.

Clients of the API should be aware that they will no longer receive specific feedback regarding resource limitations during deployment processes. They may need to update their error handling logic accordingly.

The remaining error types in the union type (ApiError<'unknown_provider'>, ApiError<'failed_to_deploy', Error[]>, and ApiError<'unknown_flow'>) are still relevant and appropriate for the PostPreBuiltDeploy endpoint.

packages/types/lib/api.ts (1)

23-24: LGTM!

The addition of the ApiError<'resource_capped'> error type to the ResDefaultErrors union is a valuable enhancement to the API's error handling capabilities. It allows the system to provide more specific feedback to clients when a resource has reached its capacity limit.

The change seamlessly integrates with the existing error types without affecting their handling, enriching the API's response options while maintaining the overall structure of the ResDefaultErrors type.

The code changes are approved.

packages/server/lib/hooks/hooks.ts (2)

54-56: LGTM!

The addition of the early return condition enhances the function's robustness and efficiency by explicitly handling the case where scriptConfigs is empty. This avoids unnecessary iterations over an empty array.


58-59: Also applies to: 61-66

packages/server/lib/routes.ts (5)

29-29: LGTM!

The import of RequestHandler type from express is approved.


88-88: LGTM!

The type annotation for the apiAuth array is approved.


89-93: LGTM!

The type annotation for the adminAuth array is approved.


94-96: LGTM!

The type annotations for the apiPublicAuth and webAuth arrays are approved.


20-20: Verify the impact of replacing authCheck with resourceCapping middleware.

Ensure that the resourceCapping middleware provides the necessary authentication and authorization checks for the API public authentication flow.

Run the following script to verify the middleware usage:

Verification successful

Verified: The resourceCapping middleware is correctly integrated into the API public authentication flow.

The resourceCapping middleware is used in the apiPublicAuth array in packages/server/lib/routes.ts, confirming its role in the authentication process. It is defined in packages/server/lib/middleware/resource-capping.middleware.ts, indicating its functionality is implemented as expected.

  • packages/server/lib/routes.ts: Usage in apiPublicAuth array.
  • packages/server/lib/middleware/resource-capping.middleware.ts: Middleware definition.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `resourceCapping` middleware in the API public authentication flow.

# Test: Search for the middleware usage in the file. Expect: Only occurrences in the `apiPublicAuth` array.
rg --type typescript $'resourceCapping'

Length of output: 73


Script:

#!/bin/bash
# Description: Verify the usage of `resourceCapping` middleware in the API public authentication flow.

# Test: Search for the middleware usage in the file. Expect: Only occurrences in the `apiPublicAuth` array.
rg --type ts 'resourceCapping'

Length of output: 499

packages/frontend/lib/index.ts (4)

100-100: LGTM!

The code changes are approved. The updated error handling mechanism enhances the specificity of the error information being passed to the AuthError, potentially allowing for more precise error handling and debugging.


312-312: LGTM!

The code changes are approved. The updated error handling mechanism is consistent with the previous code segment and enhances the specificity of the error information being passed to the AuthError.


333-333: LGTM!

The code changes are approved. The updated error handling mechanism is consistent with the previous code segments and enhances the specificity of the error information being passed to the AuthError.

Also applies to: 354-354


375-375: LGTM!

The code changes are approved. The updated error handling mechanism is consistent with the previous code segments and enhances the specificity of the error information being passed to the AuthError.

Also applies to: 396-396, 417-417

packages/webapp/src/pages/Connection/Create.tsx (1)

242-242: LGTM!

Resetting the serverErrorMessage state to an empty string when an existing integration is detected is a good practice. It clears any previous server error messages before setting up the connection configuration parameters and authentication mode based on the current integration, improving the user experience by preventing stale error messages from persisting when a valid integration is loaded.

packages/server/lib/utils/asyncWrapper.ts Show resolved Hide resolved
Copy link
Collaborator

@TBonnin TBonnin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment + a question of any generic type but looks good otherwise

@@ -51,18 +51,19 @@ export const connectionCreationStartCapCheck = async ({
}

const scriptConfigs = await getSyncConfigsWithConnections(providerConfigKey, environmentId);
if (scriptConfigs.length <= 0) {
return false;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this check is useful. If empty the for loop below will immediately exit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah indeed

packages/server/lib/utils/asyncWrapper.ts Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 037ca18 and 9ffa8c4.

Files selected for processing (1)
  • packages/server/lib/hooks/hooks.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/server/lib/hooks/hooks.ts

@bodinsamuel bodinsamuel merged commit f9f0edb into master Sep 4, 2024
23 checks passed
@bodinsamuel bodinsamuel deleted the fix/capping-connection branch September 4, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants